/**
 * Created by Administrator on 2016/9/18.
 */
define(function (require, exports) {
    var ajaxUtil = require('util/ajaxUtil').ajaxUtil;
    var logUtil = require('util/logUtil').logUtil;
    var commonUtil = require('util/commonUtil').commonUtil;

    var controls = {};
    controls.app_scene_category_detail_panel = $('#app_scene_category_detail_panel');
    controls.app_scene_category_list_panel = $('#app_scene_category_list_panel');
    controls.page_node=$('#page-wrapper');

    var urls = {};
    urls.save = '/admin/category/scene-category-save';
    urls.list = '/admin/category/scene-category-query?template_id='+window.GLOBAL_TEMPLATE_DATA.id;
    urls.delete = '/admin/category/scene-category-del';
    urls.get_page_part = '/admin/page/get-part';

    var G_MODIFY_CATEGORY_SCENE = false;
    var G_CURRENT_DATA_CATEGORY_SCENE = null;

    function init() {
        bindEvent();
        loadModelDataSceneCategory();
    }

    function bindEvent() {
        controls.app_scene_category_list_panel.find('button:eq(0)').bind('click', function () {
            ajaxUtil.doAjaxLoad(controls.page_node,urls.get_page_part,{page:'page/template/app_template.html'});
        });
        controls.app_scene_category_list_panel.find('button:eq(1)').bind('click', function () {
            resetInputSceneCategory();
            G_MODIFY_CATEGORY_SCENE = false;
            controls.app_scene_category_detail_panel.modal('show');
        });

        controls.app_scene_category_detail_panel.find('button:eq(-2)').bind('click', function () {
            var args = {};

            var val = $.trim(controls.app_scene_category_detail_panel.find('input[type=text]:eq(0)').val());
            if (!val) return commonUtil.toast('场景分类名称不能为空');
            args.val = val;
            args.template_id = window.GLOBAL_TEMPLATE_DATA.id;

            if (G_MODIFY_CATEGORY_SCENE) {
                args.id = G_CURRENT_DATA_CATEGORY_SCENE.id;
            }

            ajaxUtil.doAjaxPost(urls.save, args).done(function (ret) {
                if (ret.status == 0) {
                    controls.app_scene_category_detail_panel.modal('hide');
                    refreshSceneCategory();
                } else {
                    commonUtil.toast(ret.msg);
                }
            });


        });

        $("#app_scene_category_list_grid").on({
            'click': function () {
                var data = JSON.parse(decodeURIComponent($(this).data('user')));

                G_MODIFY_CATEGORY_SCENE = true;
                G_CURRENT_DATA_CATEGORY_SCENE = data;

                resetInputSceneCategory();
                controls.app_scene_category_detail_panel.modal('show');

                controls.app_scene_category_detail_panel.find('input[type=text]:eq(0)').val(data.val);
            }
        }, '.resource-modify');

        // 点击删除
        $("#app_scene_category_list_grid").on({
            'click': function () {
                var data = JSON.parse(decodeURIComponent($(this).data('user')));
                bootbox.confirm({
                    title: "温馨提示",
                    message: "确认删除？",
                    buttons: {
                        cancel: {
                            label: '<i class="fa fa-times"></i> 取消'
                        },
                        confirm: {
                            label: '<i class="fa fa-check"></i> 确认'
                        }
                    },
                    callback: function (result) {
                        if (result) {
                            var args = {};
                            args.id = data.id;
                            ajaxUtil.doAjaxGet(urls.delete, args).done(function (ret) {
                                if (ret.status == 0) {
                                    refreshSceneCategory();
                                } else {
                                    commonUtil.toast(ret.msg);
                                }

                            });
                        }
                    }
                });
            }
        }, '.resource-delete');
    }

    function refreshSceneCategory(data) {
        data = data || {};

        var page = $("#app_scene_category_list_grid").getGridParam('page'); // current page
        var rows = $("#app_scene_category_list_grid").getGridParam('rows'); // rows
        $("#app_scene_category_list_grid").jqGrid('setGridParam', {
            postData: data,
            page: page,
            rows: rows
        }).trigger("reloadGrid")
    }

    function loadModelDataSceneCategory() {
        $("#app_scene_category_list_grid").jqGrid({
            colModel: [
                {
                    label: '场景分类名称', name: 'val', align: 'center', formatter: function (cellValue, options, rowObject) {
                    return '<a data-user="' + encodeURIComponent(JSON.stringify(rowObject)) + '" href="javascript:;" class="resource-modify" style="color: #01AAED;">'+cellValue+'</a>&nbsp;&nbsp;'
                }
                },
                {
                    label: '创建时间',
                    name: 'create_time',
                    align: 'center',
                    formatter: function (cellValue, options, rowObject) {
                        return commonUtil.getTimeString(cellValue);

                    }
                },
                {
                    label: '操作', align: 'center', formatter: function (cellValue, options, rowObject) {
                    return '<a data-user="' + encodeURIComponent(JSON.stringify(rowObject)) + '" href="javascript:;" class="resource-delete" style="color: #01AAED;">删除</a>'
                }
                }
            ],
            styleUI: 'Bootstrap',
            datatype: 'json',
            url: urls.list,
            rowNum: 10,
            rowList: [10, 20, 30],
            viewrecords: true, // show the current page, data rang and total records on the toolbar
            height: true,
            fitColumns: true,
            width: true,
            autowidth: true,
            shrinkToFit: true,
            ajaxGridOptions: {
                dataFilter: function (ret) {
                    var retObj = JSON.parse(ret);
                    if (retObj.status == 0) {
                        return JSON.stringify(retObj.data);
                    } else {
                        commonUtil.toast(retObj.msg);
                    }
                }
            },
            pager: "#app_scene_category_list_grid_pager"
        });
        // 初始化宽宽，用于自适应宽度
        $("#app_scene_category_list_grid").setGridWidth($("#page-wrapper").width(), true);
    }


    function resetInputSceneCategory() {
        controls.app_scene_category_detail_panel.find('input[type=text]:eq(0)').val('');
    }

    exports.init = init;
});